home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_6.1 / xvor / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  5.4 KB  |  267 lines

  1. /* 
  2.  * output.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * OUTPUT.C
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include "vor.h"
  18.  
  19.  
  20. #define    XL    0.0
  21. #define    XR    (float)(WIDTH - 1)
  22. #define    YU    0.0
  23. #define    YL    (float)(HEIGHT - 1)
  24.  
  25.  
  26. extern int TRIANGULATE, DBG;
  27. extern int WRITE_FLAG;
  28.  
  29. extern FILE *fpOut;
  30.  
  31.  
  32. float pxmin, pxmax, pymin, pymax, cradius;
  33. int DISPL_PAGE = 1;
  34. int RESET = 1;                  /* when set, reset screen */
  35.  
  36. /*
  37.  * plotinit()
  38.  *   DESCRIPTION:
  39.  *     initialize various parameters
  40.  *   ARGUMENTS:
  41.  *     imgIO: image for drawing (Image *)
  42.  *   RETURN VALUE:
  43.  *     none
  44.  */
  45.  
  46. void
  47. plotinit (Image * imgIO)
  48. {
  49.   float dx, dy, d;
  50.   float xl, yl, xr, yu;
  51.  
  52.   dy = ymax - ymin;
  53.   dx = xmax - xmin;
  54.   d = (dx > dy ? dx : dy) * (float) 1.3;
  55.   xl = (float) 0.0;
  56.   yu = (float) 0.0;
  57.   xr = (float) imgIO->width - 1;
  58.   yl = (float) imgIO->height - 1;
  59.  
  60.   if ((pxmin = xmin - (d - dx) / (float) 2.0) < xl)
  61.     pxmin = (float) xl;
  62.   if ((pxmax = xmax + (d - dx) / (float) 2.0) > xr)
  63.     pxmax = (float) xr;
  64.   if ((pymin = ymin - (d - dy) / (float) 2.0) < yu)
  65.     pymin = (float) yu;
  66.   if ((pymax = ymax + (d - dy) / (float) 2.0) > yl)
  67.     pymax = (float) yl;
  68.  
  69.   cradius = (pxmax - pxmin) / (float) 350.0;
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * gprintf()
  78.  *   DESCRIPTION:
  79.  *     write to std output and, if option WRITE_FILE set, to file wbuf (stream)
  80.  *   ARGUMENTS:
  81.  *     fpOut: pointer to open FILE
  82.  *     va_list...
  83.  *   RETURN VALUE:
  84.  *     none
  85.  */
  86.  
  87. void
  88. gprintf (FILE * fpOut, char *fmt,...)
  89. {
  90.   va_list arg_ptr;
  91.  
  92.   va_start (arg_ptr, fmt);
  93.   vprintf (fmt, arg_ptr);
  94.   if (WRITE_FLAG == 1)
  95.     vfprintf (fpOut, fmt, arg_ptr);
  96.   va_end (arg_ptr);
  97. }
  98.  
  99. /*
  100.  * by construction, e->a or e->b assume the value 1.0
  101.  * (see routine bisect() in geometry.c)
  102.  */
  103. int
  104. clip_line (e, imgIO, value)
  105.      struct Edge *e;
  106.      Image *imgIO;
  107.      int value;
  108. {
  109.   struct Site *s1, *s2;
  110.   float x1, x2, y1, y2;
  111.  
  112.   if (e->a == 1.0 && e->b >= 0.0) {  /* vertical line: x = c/a */
  113.     s1 = e->ep[1];
  114.     s2 = e->ep[0];
  115.   }
  116.   else {
  117.     s1 = e->ep[0];
  118.     s2 = e->ep[1];
  119.   }
  120.  
  121.   if (e->a == 1.0) {            /* x = c - b*y */
  122.     y1 = pymin;
  123.     if ((s1 != (struct Site *) NULL) && (s1->coord.y > pymin))
  124.       y1 = s1->coord.y;
  125.     if (y1 > pymax)
  126.       return (0);
  127.     x1 = e->c - e->b * y1;
  128.  
  129.     y2 = pymax;
  130.     if ((s2 != (struct Site *) NULL) && (s2->coord.y < pymax))
  131.       y2 = s2->coord.y;
  132.     if (y2 < pymin)
  133.       return (0);
  134.     x2 = e->c - e->b * y2;
  135.  
  136.     if ((x1 > pxmax & x2 > pxmax) | (x1 < pxmin & x2 < pxmin))
  137.       return (0);
  138.  
  139.  
  140.     if (x1 > pxmax) {
  141.       x1 = pxmax;
  142.       y1 = (e->c - x1) / e->b;
  143.     }
  144.     if (x1 < pxmin) {
  145.       x1 = pxmin;
  146.       y1 = (e->c - x1) / e->b;
  147.     }
  148.     if (x2 > pxmax) {
  149.       x2 = pxmax;
  150.       y2 = (e->c - x2) / e->b;
  151.     }
  152.     if (x2 < pxmin) {
  153.       x2 = pxmin;
  154.       y2 = (e->c - x2) / e->b;
  155.     }
  156.   }
  157.   else {
  158.     x1 = pxmin;
  159.     if ((s1 != (struct Site *) NULL) && (s1->coord.x > pxmin))
  160.       x1 = s1->coord.x;
  161.     if (x1 > pxmax)
  162.       return (0);
  163.     y1 = e->c - e->a * x1;
  164.  
  165.     x2 = pxmax;
  166.     if ((s2 != (struct Site *) NULL) && (s2->coord.x < pxmax))
  167.       x2 = s2->coord.x;
  168.     if (x2 < pxmin)
  169.       return (0);
  170.     y2 = e->c - e->a * x2;
  171.  
  172.     if ((y1 > pymax & y2 > pymax) | (y1 < pymin & y2 < pymin))
  173.       return (0);
  174.  
  175.  
  176.     if (y1 > pymax) {
  177.       y1 = pymax;
  178.       x1 = (e->c - y1) / e->a;
  179.     }
  180.     if (y1 < pymin) {
  181.       y1 = pymin;
  182.       x1 = (e->c - y1) / e->a;
  183.     }
  184.     if (y2 > pymax) {
  185.       y2 = pymax;
  186.       x2 = (e->c - y2) / e->a;
  187.     }
  188.     if (y2 < pymin) {
  189.       y2 = pymin;
  190.       x2 = (e->c - y2) / e->a;
  191.     }
  192.   }
  193.   draw_line ((int) x1, (int) y1, (int) x2, (int) y2, imgIO, value);
  194. }
  195.  
  196. void
  197. out_bisector (e, imgIO, value)
  198.      struct Edge *e;
  199.      Image *imgIO;
  200.      int value;
  201. {
  202.   if (TRIANGULATE & !DBG)
  203.     draw_line ((int) e->reg[0]->coord.x, (int) e->reg[0]->coord.y, (int) e->reg[1]->coord.x, (int) e->reg[1]->coord.y, imgIO, value);
  204.  
  205.   if (!TRIANGULATE & !DBG)
  206.     gprintf (fpOut, "l %f %f %f\n", e->a, e->b, e->c);
  207.  
  208.   if (DBG)
  209.     gprintf (fpOut, "line(%d) %gx+%gy=%g, bisecting %d %d\n",
  210.     e->edgenbr, e->a, e->b, e->c, e->reg[le]->sitenbr, e->reg[re]->sitenbr);
  211. }
  212.  
  213.  
  214. void
  215. out_ep (e, imgIO, value)
  216.      struct Edge *e;
  217.      Image *imgIO;
  218.      int value;
  219. {
  220.   if (!TRIANGULATE)
  221.     clip_line (e, imgIO, value);
  222.  
  223.   if (!TRIANGULATE) {
  224.     gprintf (fpOut, "e %d", e->edgenbr);
  225.     gprintf (fpOut, " %d ", e->ep[le] != (struct Site *) NULL ? e->ep[le]->sitenbr : -1);
  226.     gprintf (fpOut, "%d ", e->ep[re] != (struct Site *) NULL ? e->ep[re]->sitenbr : -1);
  227.     gprintf (fpOut, "%d %d\n", e->reg[le]->sitenbr, e->reg[re]->sitenbr);
  228.   }
  229. }
  230.  
  231. void
  232. out_vertex (v)
  233.      struct Site *v;
  234. {
  235.   if (!TRIANGULATE & !DBG)
  236.     gprintf (fpOut, "v %f %f\n", v->coord.x, v->coord.y);
  237.   if (DBG)
  238.     gprintf (fpOut, "vertex(%d) at %f %f\n", v->sitenbr, v->coord.x, v->coord.y);
  239. }
  240.  
  241.  
  242. void
  243. out_site (s, imgIO, value)
  244.      struct Site *s;
  245.      Image *imgIO;
  246.      int value;
  247. {
  248.   if (!TRIANGULATE & !DBG)
  249.     draw_circle ((int) s->coord.x, (int) s->coord.y, (int) cradius, imgIO, value);
  250.   if (!TRIANGULATE & !DBG)
  251.     gprintf (fpOut, "s %f %f\n", s->coord.x, s->coord.y);
  252.   if (DBG)
  253.     gprintf (fpOut, "site(%d) at %f %f\n", s->sitenbr, s->coord.x, s->coord.y);
  254. }
  255.  
  256.  
  257. void
  258. out_triple (s1, s2, s3)
  259.      struct Site *s1, *s2, *s3;
  260. {
  261.   if (TRIANGULATE & !DBG)
  262.     gprintf (fpOut, "%d %d %d\n", s1->sitenbr, s2->sitenbr, s3->sitenbr);
  263.   if (DBG)
  264.     printf ("circle through left=%d right=%d bottom=%d\n",
  265.             s1->sitenbr, s2->sitenbr, s3->sitenbr);
  266. }
  267.